package top.healthylife.gateway.platform.eye.api.api.renyuanfengxian;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.basics.utils.utils.Get;
import top.healthylife.basics.utils.utils.SystemThreadLocal;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.core.util.SystemConfigUtils;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.dto.request.req.ApiReqDto;
import top.healthylife.gateway.feignapi.utils.ApiInvokeHelper;
import top.healthylife.gateway.platform.eye.api.config.SystemConfig;
import top.healthylife.gateway.platform.eye.api.util.OutHttpUtil;
import top.healthylife.gateway.platform.eye.dto.BaseRes.EyeResBaseResultDto;
import top.healthylife.gateway.platform.eye.dto.renyuanfengxian.EyeHumanZhiXingInfoDto.req.EyeReqHumanZhiXingInfoDto;
import top.healthylife.gateway.platform.eye.dto.renyuanfengxian.EyeHumanZhiXingInfoDto.res.EyeResHumanZhiXingInfoDto;
import top.heathylife.gateway.common.cons.SystemCons;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author: gzx
* @date: 2020-12-15 19:07:25
*/
@Slf4j
@Service
@ApiServiceExplain(price = 150,docUrl ="http://open.tianyancha.com/open/1077",apiName = "被执行人（人员）[1077]", info = "可以通过公司名称或ID和人名获取被执行人信息，被执行人信息包括执行法院、案件内容、执行标的、被执行人名称、组织机构代码等字段的详细信息")
public class EyeHumanZhiXingInfoApi implements ParentFace<EyeReqHumanZhiXingInfoDto, EyeResHumanZhiXingInfoDto> {

    static final String URL = "http://open.api.tianyancha.com/services/v4/open/human/zhixinginfo";

    /**
     * 执行这个服务接口
     * 如果成功，返回成功结果，如果接口错误，直接抛异常即可
     *
     * @param eyeDto 请求参数
     * @return 查询的真实结果，如果是失败，则直接返回异常即可
     */
    @Override
    public EyeResHumanZhiXingInfoDto execute(EyeReqHumanZhiXingInfoDto eyeDto) {
        //可直接通过工具类获取这个对象
        SystemConfig o = (SystemConfig) SystemConfigUtils.get();
        return OutHttpUtil.get(URL, BeanUtil.beanToMap(eyeDto), o, EyeResHumanZhiXingInfoDto.class);
    }



    /**
     * 获取全量记录
     *
     * @param params 入参
     * @return
     */
    public <T extends EyeResBaseResultDto<M>,M> T getAllRecord(ApiReqDto params) {
        Map<String, Object> map = BeanUtil.beanToMap(params);
        String secret = SystemThreadLocal.get(SystemCons.secret);
        EyeReqHumanZhiXingInfoDto reqDto = new EyeReqHumanZhiXingInfoDto();
        reqDto.setPageSize(20);
        reqDto.setPageNum(1);
        reqDto.setName(StrUtil.toString(map.get(Get.name(EyeReqHumanZhiXingInfoDto::getName))));
        reqDto.setHumanName(StrUtil.toString(map.get(Get.name(EyeReqHumanZhiXingInfoDto::getHumanName))));
        reqDto.setCid((Long) map.get(Get.name(EyeReqHumanZhiXingInfoDto::getCid)));
        reqDto.setHid((Long) map.get(Get.name(EyeReqHumanZhiXingInfoDto::getHid)));

        //首次调用获取总数,计算分页
        T resDto = ApiInvokeHelper.results(
                SystemCons.TYC_SHORT_NAME,
                this.getClass().getAnnotation(ApiServiceExplain.class).apiName(),
                reqDto,
                secret);

        // 结果集
        List<M> allRecord1 = new ArrayList<>(resDto.getItems());

        Integer total = resDto.getTotal();
        Integer pageSize = reqDto.getPageSize();
        int totalPages = (total + pageSize - 1) / pageSize;

        for (int i = 2; i <= totalPages; i++) {
            reqDto.setPageNum(i);
            resDto = ApiInvokeHelper.results(
                    SystemCons.TYC_SHORT_NAME,
                    this.getClass().getAnnotation(ApiServiceExplain.class).apiName(),
                    reqDto,
                    secret);
            allRecord1.addAll(resDto.getItems());
        }
        resDto.setItems(allRecord1);
        return resDto;
    }
}
